<?php
namespace app\jobmanagement\controller;
use app\constdir\ApiConst;
use app\jobmanagement\service\Common;
use app\jobmanagement\service\Super as sSuper;

class Super extends Base
{
    private $sSuper;
    public function __construct(){
        parent::__construct();
        $this->sSuper = new sSuper();
        if(session('is_sys') != 1){
            echo $this->apiError(ApiConst::AUTH_ADMIN_USER_ERROR_MSG,ApiConst::AUTH_ADMIN_USER_ERROR_CODE);
            exit();
        }
    }

    /**
     * @auther zlq
     * @create_time 2020/6/10 10:13
     * @description 获取添加管理员参数
     * @return \think\Collection|\think\model\Collection
     */
    public function addAdminParam()
    {
        // 提供能选择的区域
        $plant = getDicList('plant_area');
        return $this->apiSuccess('success', $plant);
    }

    /**
     * @auther zlq
     * @create_time 2020/6/9 18:07
     * @description 添加管理员
     */
    public function saveAdmin() {
        $param = [
            'user_id' => input('user_id',''),
            'dept_id' => input('dept_id',0)
        ];
        $user_array = explode(',', trim($param['user_id'], ','));

        $user_array = array_filter($user_array);

        if (empty($user_array) or !is_array($user_array)) {
            return $this->apiError(ApiConst::ADD_ADMIN_USER_ID_ERROR_MSG,ApiConst::ADD_ADMIN_USER_ID_ERROR_CODE);
        }

        if ($param['dept_id'] < 1) {
            return $this->apiError(ApiConst::ADD_ADMIN_DEPT_ERROR_MSG,ApiConst::ADD_ADMIN_DEPT_ERROR_CODE);
        }

        $adminSave =  $this->sSuper->adminSave($user_array , $param['dept_id']);
        if ($adminSave) {
            return $this->apiSuccess('管理员添加成功');
        } else {
            return $this->apiError('管理员添加失败,请重试');
        }
    }



    /**
     * @auther zlq
     * @create_time 2020/6/10 8:34
     * @description 管理员列表
     * @return \think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAdminList()
    {
        // 获取管理员列表
        $CommonService = new Common();
        $list = $CommonService->getAdminList();
        return $this->apiSuccess('success', $list);
    }


    /**
     * @introduction:删除管理员
     * @return \think\Collection|\think\model\Collection
     * @throws \think\exception\PDOException
     * @author:cxh
     * @date: 2020/6/10 9:45
     */
    public function deleteAdmin(){
        $param = [
            'id' => input('user_id',0),
        ];
        if (empty($param['id'])) {
            return $this->apiError(ApiConst::DELETE_ADMIN_USER_ID_ERROR_MSG,ApiConst::DELETE_ADMIN_USER_ID_ERROR_CODE);
        }
        $info =  $this->sSuper->deleteAdmin($param);
        if ($info) {
            return $this->apiSuccess('管理员删除成功');
        } else {
            return $this->apiError('管理员删除失败,请重试');
        }

    }


    /**
     * @introduction:保存阶梯计费审核员
     * @return \think\Collection|\think\model\Collection
     * @throws \think\exception\PDOException
     * @author:cxh
     * @date: 2020/7/27 15:53
     */
    public function saveLadderAudit() {
        $param = [
            'user_id' => input('user_id','')
        ];
        $user_array = explode(',', trim($param['user_id'], ','));

        $user_array = array_filter($user_array);

        if (empty($user_array) or !is_array($user_array)) {
            return $this->apiError(ApiConst::ADD_ADMIN_USER_ID_ERROR_MSG,ApiConst::ADD_ADMIN_USER_ID_ERROR_CODE);
        }

        $adminSave =  $this->sSuper->adminSave($user_array , 0 , 9);
        if ($adminSave) {
            return $this->apiSuccess('阶梯计费审核员添加成功');
        } else {
            return $this->apiError('添加失败,请重试');
        }
    }


    /**
     * @introduction:获取阶梯计费审核员列表
     * @return \think\Collection|\think\model\Collection
     * @author:cxh
     * @date: 2020/7/28 16:15
     */
    public function ladderAuditList()
    {
        // 获取阶梯计费审核员列表
        $list = $this->sSuper->ladderAuditList(['level'=>9]);
        return $this->apiSuccess('success', $list);
    }


    /**
     * @introduction:删除阶梯计费审核员
     * @return \think\Collection|\think\model\Collection
     * @throws \think\exception\PDOException
     * @author:cxh
     * @date: 2020/7/28 16:17
     */
    public function deleteLadderAudit(){
        $param = [
            'id' => input('user_id',0),
        ];
        if (empty($param['id'])) {
            return $this->apiError(ApiConst::DELETE_ADMIN_USER_ID_ERROR_MSG,ApiConst::DELETE_ADMIN_USER_ID_ERROR_CODE);
        }
        $info =  $this->sSuper->deleteAdmin($param);
        if ($info) {
            return $this->apiSuccess('删除成功');
        } else {
            return $this->apiError('删除失败,请重试');
        }

    }



}